/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.apidoc.utility;

import java.io.IOException;
import java.util.Properties;

import com.hyts.apidoc.config.ApiDocConfig;

/** 
 * @title PropUtils.java  
 * @package com.hyts.apidoc.utility  
 * @description 解析相关配置文件，读取配置文件工具类
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年4月18日 
 * @version V1.0 
 */
public final class PropUtils {
	
	/**  
	 * <p>系统中唯一配置文件定义对象</p> 
	 * @fieldName prop
	 * @fieldType Properties
	 */ 
	private static Properties prop = null;
	
	/**  
	 * <p>
	 * 	读取配置文件路径：默认为{@link com.hyts.apidoc.config.ApiDocConfig #DEFAULT_API_CONFIG_FILE} 
	 * 	字段默认值，可以 load方法修改默认读取文件操作，但只能在初始化之前修改，否则无效，而且同时只可以维护一个
	 * 	文件，切记。
	 * </p> 
	 * @fieldName loadConfigPath
	 * @fieldType String
	 */ 
	private static String loadConfigPath = ApiDocConfig.DEFAULT_API_CONFIG_FILE;
	
	
	//只执行一次-故此不需要进行判断是否为空操作与prop对象属于统一体
	static {
		prop = new Properties();
		try {
			prop.load(PropUtils.class.getClassLoader().getResourceAsStream(loadConfigPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**  
	 * <b>newInstance</b>
	 * <p>构建静态properties对象</p>
	 * @exception
	 */
	@Deprecated
	public static void newInstance(){
		if(prop == null){
			synchronized(PropUtils.class) {
				if(prop == null) {
					prop = new Properties();
				}
			}	
		}
	}
	
	/**  
	 * <b>reload</b>
	 * <p>重新切换读取配置文件操作</p>
	 * @param configFile
	 * @exception
	 */ 
	public static void reload(String configFile) {
		try {
			prop.load(PropUtils.class.getClassLoader().getResourceAsStream(configFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**  
	 * <b>getValue</b>
	 * <p>根据key获取value值</p>
	 * @param key
	 * @return 对应的value值
	 * @exception
	 */ 
	public static String getValue(String key) {
		return prop.getProperty(key, "");
	}
	
}
